﻿namespace Leo.Common
{
    public class EnumHelper
    {
        public static Dictionary<string, ValueType> ToDictionary<ValueType>(Type type, string defaultName = "", ValueType defaultValue = default(ValueType))
        {
            if (type == null)
            {
                throw new ArgumentException("传入的枚举类型无效");
            }
            if (!type.IsEnum)
            {
                throw new ArgumentException("类型" + type.FullName + " 不是有效的枚举类型");
            }
            Dictionary<string, ValueType> dictionary = new Dictionary<string, ValueType>();
            if (defaultName != string.Empty)
            {
                dictionary.Add(defaultName, defaultValue);
            }
            foreach (object obj in Enum.GetValues(type))
            {
                string text = obj.ToString();
                ValueType valueType = (ValueType)((object)Convert.ChangeType(Convert.ToInt32(obj), typeof(ValueType)));
                dictionary.Add(text, valueType);
            }
            return dictionary;
        }

        public static Dictionary<string, ValueType> ToDictionary<EnumType, ValueType>(string defaultName = "", ValueType defaultValue = default(ValueType))
        {
            return EnumHelper.ToDictionary<ValueType>(typeof(EnumType), defaultName, defaultValue);
        }

        public static Dictionary<string, int> ToDictionary<EnumType>(string defaultName = "", int defaultValue = -1)
        {
            return EnumHelper.ToDictionary<EnumType, int>(defaultName, defaultValue);
        }

        public static List<NameValue<string, ValueType>> ToNameValues<ValueType>(Type type, string defaultName = "", ValueType defaultValue = default(ValueType))
        {
            List<NameValue<string, ValueType>> list = new List<NameValue<string, ValueType>>();
            Dictionary<string, ValueType> dictionary = EnumHelper.ToDictionary<ValueType>(type, defaultName, defaultValue);
            foreach (string text in dictionary.Keys)
            {
                NameValue<string, ValueType> nameValue = NameValue.Instance<string, ValueType>(text, dictionary[text]);
                list.Add(nameValue);
            }
            return list;
        }
        public static List<NameValue<string, ValueType>> ToNameValues<EnumType, ValueType>(string defaultName = "", ValueType defaultValue = default(ValueType))
        {
            return EnumHelper.ToNameValues<ValueType>(typeof(EnumType), defaultName, defaultValue);
        }

        public static List<NameValue<string, int>> ToNameValues<EnumType>(string defaultName = "", int defaultValue = -1)
        {
            return EnumHelper.ToNameValues<EnumType, int>(defaultName, defaultValue);
        }

        public static List<TextValue<string, ValueType>> ToTextValues<ValueType>(Type type, string defaultText = "", ValueType defaultValue = default(ValueType))
        {
            List<TextValue<string, ValueType>> list = new List<TextValue<string, ValueType>>();
            Dictionary<string, ValueType> dictionary = EnumHelper.ToDictionary<ValueType>(type, defaultText, defaultValue);
            foreach (string text in dictionary.Keys)
            {
                TextValue<string, ValueType> textValue = TextValue.Instance<string, ValueType>(text, dictionary[text]);
                list.Add(textValue);
            }
            return list;
        }
        public static List<TextValue<string, ValueType>> ToTextValues<EnumType, ValueType>(string defaultText = "", ValueType defaultValue = default(ValueType))
        {
            return EnumHelper.ToTextValues<ValueType>(typeof(EnumType), defaultText, defaultValue);
        }

        public static List<TextValue<string, int>> ToTextValues<EnumType>(string defaultText = "", int defaultValue = -1)
        {
            return EnumHelper.ToTextValues<EnumType, int>(defaultText, defaultValue);
        }

        public static List<KeyValue<string, ValueType>> ToKeyValues<ValueType>(Type type, string defaultKey = "", ValueType defaultValue = default(ValueType))
        {
            List<KeyValue<string, ValueType>> list = new List<KeyValue<string, ValueType>>();
            Dictionary<string, ValueType> dictionary = EnumHelper.ToDictionary<ValueType>(type, defaultKey, defaultValue);
            foreach (string text in dictionary.Keys)
            {
                KeyValue<string, ValueType> keyValue = KeyValue.Instance<string, ValueType>(text, dictionary[text]);
                list.Add(keyValue);
            }
            return list;
        }

        public static List<KeyValue<string, ValueType>> ToKeyValues<EnumType, ValueType>(string defaultKey = "", ValueType defaultValue = default(ValueType))
        {
            return EnumHelper.ToKeyValues<ValueType>(typeof(EnumType), defaultKey, defaultValue);
        }

        public static List<KeyValue<string, int>> ToKeyValues<EnumType>(string defaultKey = "", int defaultValue = -1)
        {
            return EnumHelper.ToKeyValues<EnumType, int>(defaultKey, defaultValue);
        }
    }
}
